package edu.uc.ui.ctrl25;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.liuvei.common.PagerItem;
import com.liuvei.common.SysFun;

import edu.uc.bean.Member;
import edu.uc.bean.NewsCat;
import edu.uc.service.MemberService;

@Repository("MemberAction")
@Scope("prototype")
public class MemberAction  extends BaseAction{
	
	@Autowired
	private MemberService memberService;
	private String id;
	private String userId;
	private String nickName;
	private String userName;
	private String userPass;
	private String email;
	private java.util.Date regDate;
	public String list(){
		List<Member> vList = null;
		PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);
		
		Long rowCount = memberService.count();
	
		pagerItem.changeRowCount(rowCount);
	
		vList = memberService.pager(pagerItem.getPageNum(),pagerItem.getPageSize());
		
		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));
		
		request.put("pagerItem", pagerItem);
		request.put("dataList", vList);
		for(int i = 0; i<vList.size();i++){
			System.out.println(vList.get(i).getEmail());
		}
		return "list";
	} 
	public String listDeal() throws UnsupportedEncodingException{
		
		
		String str = new String(nickName.getBytes("iso-8859-1"),"UTF-8");
		request.put("nickName", str);
		if(!SysFun.isNullOrEmpty(str)){
			nickName = str.trim();
		}
		else{
			nickName = "";
		}
		System.out.println(nickName+123);
		List<Member> vList = null;
		
		PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);
		
		Long rowCount = memberService.countByName(nickName);
		
		pagerItem.changeRowCount(rowCount);
	
		
		vList = memberService.pagerByName(nickName,pagerItem.getPageNum(),pagerItem.getPageSize());
		
		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));
		
		request.put("pagerItem", pagerItem);
		request.put("dataList", vList);
		return "list";
	}
	public String insert(){
		return "insert";
	}
	public String insertDeal(){
		regDate = new Date();
		request.put("nickName", nickName);
		request.put("userName", userName);
		request.put("userPass", userPass);
		request.put("email", email);
		request.put("regDate", regDate);
		String vMsg="";
		if(SysFun.isNullOrEmpty(userName)){
			vMsg+="用户名不能为空";
		}
		if(SysFun.isNullOrEmpty(nickName)){
			vMsg+="昵称不能为空";
		}
		if(SysFun.isNullOrEmpty(userPass)){
			vMsg+="密码不能为空";
		}
		if(SysFun.isNullOrEmpty(email)){
			vMsg+="邮箱不能为空";
		}
		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "insert";
		}
		Member bean = new Member();
		bean.setEmail(email);
		bean.setNickName(nickName);
		bean.setUserName(userName);
		bean.setUserPass(userPass);
		bean.setRegDate(regDate);
		Long result = 0L;
		try{
			result = memberService.insert(bean);
		}
		catch (Exception e) {
			// TODO: handle exception
			vMsg +="新建失败"+e.getMessage();
		}
		if(result >0){
			return "go_preload";
		}else{
			request.put("msg", vMsg);
			return "insert";
		}
	}
	public String delete(){
		return "go_ok";
	}
	public String deleteDeal(){
		if(!SysFun.isNullOrEmpty(id)){
			Long iId = SysFun.parseLong(id);
			Long result = memberService.delete(iId);
			if(result >0){
				return "go_ok";
			}
		
		}
		return "go_no";
	}
	public String update(){
		if(!SysFun.isNullOrEmpty(id)) {
			Long iId = SysFun.parseLong(id);
			Member bean = memberService.load(iId);
			if(bean != null) {
				System.out.println(bean.getUserId());
				request.put("userId", bean.getUserId());
				request.put("nickName", bean.getNickName());
				request.put("email", bean.getEmail());
				return "update";	
			}
		}
		return "update";
	}
	public String updateDeal(){
		Long result = 0L;
		String vMsg="";
		request.put("nickName", nickName);
		request.put("email", email);
		request.put("userId", userId);
		if(SysFun.isNullOrEmpty(nickName)){
			vMsg+="昵称不能为空";
		}
		if(SysFun.isNullOrEmpty(email)){
			vMsg+="邮箱不能为空";
		}
		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "update";
		}
		System.out.println(userId+"1111111111111");
		Long iId = SysFun.parseLong(userId);
		Member bean = memberService.load(iId);
		System.out.println(email+"111111");
		System.out.println(nickName+"222222");
		
		if(bean == null){
			vMsg +="没有查询到该数据";
		}
		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "update";
		}
		bean.setEmail(email);
		bean.setNickName(nickName);

	
	
		try{
			result = memberService.update(bean);
		}
		catch (Exception e) {
			// TODO: handle exception
			vMsg +="更新失败"+e.getMessage();
		}
		if(result >0){
			return "go_preload";
		}else{
			request.put("msg", vMsg);
			return "update";
		}
	}
	
	public String editpass(){
		if(!SysFun.isNullOrEmpty(id)) {
			Long iId = SysFun.parseLong(id);
			Member bean = memberService.load(iId);
			if(bean != null) {
				System.out.println(bean.getUserId());
				request.put("userId", bean.getUserId());
				request.put("userPass", bean.getUserPass());
				return "editpass";	
			}
		}
		return "editpass";
	}
	public String editpassDeal(){
		Long result = 0L;
		String vMsg="";
		request.put("userPass", userPass);
		request.put("userId", userId);
		if(SysFun.isNullOrEmpty(userPass)){
			vMsg+="密码不能为空";
		}
		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "editpass";
		}
		System.out.println(userId+"1111111111111");
		Long iId = SysFun.parseLong(userId);
		Member bean = memberService.load(iId);
		System.out.println(email+"111111");
		System.out.println(nickName+"222222");
		
		if(bean == null){
			vMsg +="没有查询待该数据";
		}
		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "editpass";
		}
		bean.setUserPass(userPass);
		try{
			result = memberService.update(bean);
		}
		catch (Exception e) {
			// TODO: handle exception
			vMsg +="更新失败"+e.getMessage();
		}
		if(result >0){
			return "go_preload";
		}else{
			request.put("msg", vMsg);
			return "editpass";
		}
	}
	public String detail(){
		if(!SysFun.isNullOrEmpty(id)) {
			String vMsg="";
			Long iId = SysFun.parseLong(id);
			Member bean = memberService.load(iId);
			if(bean != null) {
				request.put("userId", bean.getUserId());
				request.put("userName", bean.getUserName());
				request.put("nickName", bean.getNickName());
				request.put("email", bean.getEmail());
				request.put("regDate", bean.getRegDate());
				return "detail";	
			}
			else{
				vMsg+="没有找到该数据";
			}
		}
		return "detail";

	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPass() {
		return userPass;
	}
	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public java.util.Date getRegDate() {
		return regDate;
	}
	public void setRegDate(java.util.Date regDate) {
		this.regDate = regDate;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}


}
